//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using EntLibContrib.Query.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design.Validation;

namespace EntLibContrib.Query.Configuration
{
	/// <summary>
	/// Configuration data defining CustomCommandData. This configuration section defines the name and type
	/// of the ICustomCommand class
	/// </summary>
    [ResourceDisplayName(typeof(DesignResources), "CustomCommandDataDisplayName")]
    [ResourceDescription(typeof(DesignResources), "CustomCommandDataDescription")]
    [TypePickingCommand(
        TitleResourceType = typeof(DesignResources),
        TitleResourceName = "CustomCommandDataDisplayName",
        Replace = CommandReplacement.DefaultAddCommandReplacement)]
    public class CustomCommandData : CommandData, IHelperAssistedCustomConfigurationData<CustomCommandData>
	{
		private static readonly AssemblyQualifiedTypeNameConverter typeConverter = new AssemblyQualifiedTypeNameConverter();
		
        private readonly CustomProviderDataHelper<CustomCommandData> helper;

        /// <summary>
        /// Initializes with default values.
        /// </summary>
        public CustomCommandData()
        {
            this.helper = new CustomProviderDataHelper<CustomCommandData>(this);
        }

        /// <summary>
        /// Initializes with name and provider type.
        /// </summary>
        public CustomCommandData(string name, Type type)
        {
            this.helper = new CustomProviderDataHelper<CustomCommandData>(this);
            this.Name = name;
            this.Type = type;
        }

        /// <summary>
        /// Initializes with name and fully qualified type name of the provider type.
        /// </summary>
        public CustomCommandData(string name, string typeName)
        {
            this.helper = new CustomProviderDataHelper<CustomCommandData>(this);
            this.Name = name;
            this.TypeName = typeName;
        }

        /// <summary>
        /// Name of the data provider.
        /// </summary>
        [Browsable(false)]
        [ConfigurationProperty(nameProperty, DefaultValue = "Custom Command")]
        public override string Name
        {
            get { return base.Name; }
            set { base.Name = value; }
        }

        /// <summary>
        /// Gets a <see cref="ConfigurationPropertyCollection"/> of the properties that are defined for 
        /// this configuration element when implemented in a derived class. 
        /// </summary>
        /// <value>
        /// A <see cref="ConfigurationPropertyCollection"/> of the properties that are defined for this
        /// configuration element when implemented in a derived class. 
        /// </value>
        protected override ConfigurationPropertyCollection Properties
        {
            get { return this.helper.Properties; }
        }

        /// <summary>
        /// Overridden in order to apply <see cref="BrowsableAttribute"/>.
        /// </summary>
        [Browsable(true)]
        [Editor(CommonDesignTime.EditorTypes.TypeSelector, CommonDesignTime.EditorTypes.UITypeEditor)]
        [BaseType(typeof(ICommand), typeof(CustomCommandData))]
        [ResourceDescription(typeof(DesignResources), "CustomCommandDataTypeNameDescription")]
        [ResourceDisplayName(typeof(DesignResources), "CustomCommandDataTypeNameDisplayName")]
        public override string TypeName
        {
            get { return base.TypeName; }
            set { base.TypeName = value; }
        }

        #region IHelperAssistedCustomConfigurationData<CustomCommandData> Members

        /// <summary>
        /// Gets or sets custom configuration attributes.
        /// </summary>
        [Validation(QueryDesignTime.ValidatorTypes.NameValueCollectionValidator)]
        public NameValueCollection Attributes
        {
            get { return this.helper.Attributes; }
        }

        /// <summary>
        /// Gets the helper.
        /// </summary>
        CustomProviderDataHelper<CustomCommandData>
            IHelperAssistedCustomConfigurationData<CustomCommandData>.Helper
        {
            get { return this.helper; }
        }

        /// <summary>Invokes the inherited behavior.</summary>
        object IHelperAssistedCustomConfigurationData<CustomCommandData>.BaseGetPropertyValue(
            ConfigurationProperty property)
        {
            return base[property];
        }

        /// <summary>Invokes the inherited behavior.</summary>
        void IHelperAssistedCustomConfigurationData<CustomCommandData>.BaseSetPropertyValue(
            ConfigurationProperty property, object value)
        {
            base[property] = value;
        }

        /// <summary>Invokes the inherited behavior.</summary>
        void IHelperAssistedCustomConfigurationData<CustomCommandData>.BaseUnmerge(
            ConfigurationElement sourceElement,
            ConfigurationElement parentElement,
            ConfigurationSaveMode saveMode)
        {
            base.Unmerge(sourceElement, parentElement, saveMode);
        }

        /// <summary>Invokes the inherited behavior.</summary>
        void IHelperAssistedCustomConfigurationData<CustomCommandData>.BaseReset(
            ConfigurationElement parentElement)
        {
            base.Reset(parentElement);
        }

        /// <summary>Invokes the inherited behavior.</summary>
        bool IHelperAssistedCustomConfigurationData<CustomCommandData>.BaseIsModified()
        {
            return base.IsModified();
        }

        #endregion

        /// <summary>
        /// Sets the attribute value for a key.
        /// </summary>
        /// <param name="key">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        public void SetAttributeValue(string key, string value)
        {
            this.helper.HandleSetAttributeValue(key, value);
        }

        /// <summary>
        /// Modifies the <see cref="CustomCommandData"/> object to remove all values that should not be saved. 
        /// </summary>
        /// <param name="sourceElement">A <see cref="ConfigurationElement"/> object at the current level containing a merged view of the properties.</param>
        /// <param name="parentElement">A parent <see cref="ConfigurationElement"/> object or <see langword="null"/> if this is the top level.</param>
        /// <param name="saveMode">One of the <see cref="ConfigurationSaveMode"/> values.</param>
        protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
        {
            this.helper.HandleUnmerge(sourceElement, parentElement, saveMode);
        }

        /// <summary>
        /// Resets the internal state of the <see cref="CustomCommandData"/> object, 
        /// including the locks and the properties collection.
        /// </summary>
        /// <param name="parentElement">The parent element.</param>
        protected override void Reset(ConfigurationElement parentElement)
        {
            this.helper.HandleReset(parentElement);
        }

        /// <summary>
        /// Indicates whether this configuration element has been modified since it was last 
        /// saved or loaded when implemented in a derived class.
        /// </summary>
        /// <returns><see langword="true"/> if the element has been modified; otherwise, <see langword="false"/>. </returns>
        protected override bool IsModified()
        {
            return this.helper.HandleIsModified();
        }

        /// <summary>
        /// Called when an unknown attribute is encountered while deserializing the <see cref="CustomCommandData"/> object.
        /// </summary>
        /// <param name="name">The name of the unrecognized attribute.</param>
        /// <param name="value">The value of the unrecognized attribute.</param>
        /// <returns><see langword="true"/> if the processing of the element should continue; otherwise, <see langword="false"/>.</returns>
        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            return this.helper.HandleOnDeserializeUnrecognizedAttribute(name, value);
        }

        /// <summary>
        /// Creates a <see cref="TypeRegistration"/> instance describing the provider represented by 
        /// this configuration object.
        /// </summary>
        /// <returns>The type registration for the custom exception handler</returns>
        public override TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration(
                    RegistrationExpressionBuilder.BuildExpression(this.Type, this.Attributes),
                    typeof(ICommand))
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}
